<template>
  <div class="text-counter">
    <el-card class="tool-card" shadow="hover">
      <template #header>
        <div class="card-header">
          <h3>字符统计工具</h3>
          <p class="card-description">快速统计文本的字符数、单词数、行数和段落数</p>
        </div>
      </template>
      
      <div class="tool-content">
        <div class="input-section">
          <el-form label-position="top">
            <el-form-item label="输入文本">
              <el-input
                v-model="inputText"
                type="textarea"
                :rows="8"
                placeholder="请输入要统计的文本..."
                @input="updateStats"
              />
            </el-form-item>
          </el-form>
        </div>
        
        <div class="options-section">
          <h4 class="section-title">统计选项</h4>
          <div class="options-wrapper">
            <el-checkbox v-model="ignoreSpaces">忽略空格</el-checkbox>
            <el-checkbox v-model="ignoreLineBreaks">忽略换行</el-checkbox>
          </div>
        </div>
        
        <div class="result-section">
          <h4 class="section-title">统计结果</h4>
          <div class="stats-container">
            <div class="stat-item">
              <span class="stat-label">字符数(含空格)</span>
              <span class="stat-value">{{ previewData.characters }}</span>
            </div>
            <div class="stat-item">
              <span class="stat-label">字符数(不含空格)</span>
              <span class="stat-value">{{ previewData.characterNoSpaces }}</span>
            </div>
            <div class="stat-item">
              <span class="stat-label">单词数</span>
              <span class="stat-value">{{ previewData.words }}</span>
            </div>
            <div class="stat-item">
              <span class="stat-label">中文字符</span>
              <span class="stat-value">{{ previewData.chineseChars }}</span>
            </div>
            <div class="stat-item">
              <span class="stat-label">行数</span>
              <span class="stat-value">{{ previewData.lines }}</span>
            </div>
            <div class="stat-item">
              <span class="stat-label">段落数</span>
              <span class="stat-value">{{ previewData.paragraphs }}</span>
            </div>
          </div>
        </div>
        
        <div class="actions-section">
          <el-button type="primary" @click="copyResult" :disabled="!inputText">复制结果</el-button>
          <el-button @click="clearText" :disabled="!inputText">清空文本</el-button>
        </div>
      </div>
      
      <div class="guide-section">
        <h4 class="section-title">快速使用指南</h4>
        <div class="guide-container">
          <div class="guide-step" v-for="(step, index) in usageSteps" :key="index">
            <div class="step-number">{{ index + 1 }}</div>
            <div class="step-content">
              <p class="step-text">{{ step }}</p>
            </div>
          </div>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue';
import { ElMessage } from 'element-plus';

const inputText = ref('');
const ignoreSpaces = ref(false);
const ignoreLineBreaks = ref(false);

const previewData = ref({
  characters: 0,
  characterNoSpaces: 0,
  words: 0,
  chineseChars: 0,
  lines: 0,
  paragraphs: 0
});

const usageSteps = [
  '在输入框中粘贴或输入需要统计的文本',
  '根据需要选择是否忽略空格或换行符',
  '查看统计结果',
  '使用"复制结果"按钮复制统计结果'
];

// 更新统计数据
const updateStats = () => {
  let text = inputText.value;
  
  // 原始字符数
  const originalLength = text.length;
  
  // 忽略空格的处理
  if (ignoreSpaces.value) {
    text = text.replace(/\s/g, '');
  }
  
  // 行数计算
  const lines = ignoreLineBreaks.value 
    ? 1 
    : (inputText.value.match(/\n/g) || []).length + 1;
  
  // 单词数计算（中英文混合统计）
  const words = text.trim().split(/\s+/).filter(word => word.length > 0).length;
  
  // 中文字符计算
  const chineseChars = (text.match(/[\u4e00-\u9fa5]/g) || []).length;
  
  // 段落计算
  const paragraphs = inputText.value.split(/\n\s*\n/).filter(p => p.trim().length > 0).length;
  
  previewData.value = {
    characters: originalLength,
    characterNoSpaces: text.length,
    words: words,
    chineseChars: chineseChars,
    lines: lines,
    paragraphs: paragraphs
  };
};

// 复制结果
const copyResult = () => {
  const result = `字符数(含空格): ${previewData.value.characters}
字符数(不含空格): ${previewData.value.characterNoSpaces}
单词数: ${previewData.value.words}
中文字符: ${previewData.value.chineseChars}
行数: ${previewData.value.lines}
段落数: ${previewData.value.paragraphs}`;

  navigator.clipboard.writeText(result)
    .then(() => {
      ElMessage.success('统计结果已复制');
    })
    .catch(() => {
      ElMessage.error('复制失败');
    });
};

// 清空文本
const clearText = () => {
  inputText.value = '';
  updateStats();
};

// 监听选项变化
watch([ignoreSpaces, ignoreLineBreaks], () => {
  updateStats();
});

// 初始化
updateStats();
</script>

<style scoped>
.text-counter {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
}

.tool-card {
  border-radius: 8px;
  margin-bottom: 20px;
  transition: all 0.3s ease;
}

.card-header {
  padding-bottom: 15px;
}

.card-header h3 {
  font-size: 18px;
  color: #303133;
  margin: 0 0 8px 0;
  font-weight: 600;
}

.card-description {
  font-size: 14px;
  color: #606266;
  margin: 0;
}

.tool-content {
  padding: 5px 0;
}

.section-title {
  font-size: 16px;
  color: #303133;
  margin-bottom: 15px;
  font-weight: 500;
  position: relative;
  padding-left: 12px;
}

.section-title:before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 4px;
  height: 16px;
  background-color: #3498db;
  border-radius: 2px;
}

.input-section {
  margin-bottom: 25px;
}

.options-section {
  margin-bottom: 25px;
}

.options-wrapper {
  display: flex;
  gap: 20px;
  flex-wrap: wrap;
}

.result-section {
  margin-bottom: 25px;
  background-color: #f8f9fa;
  border-radius: 8px;
  padding: 20px;
}

.stats-container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 15px;
}

.stat-item {
  padding: 10px;
  background-color: #fff;
  border-radius: 6px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.06);
}

.stat-label {
  display: block;
  font-size: 14px;
  color: #606266;
  margin-bottom: 5px;
}

.stat-value {
  display: block;
  font-size: 20px;
  color: #303133;
  font-weight: 600;
}

.actions-section {
  margin: 25px 0;
  display: flex;
  gap: 10px;
}

.guide-section {
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px dashed #e4e7ed;
}

.guide-container {
  display: flex;
  justify-content: space-between;
  gap: 15px;
  margin-top: 20px;
}

.guide-step {
  flex: 1;
  background-color: #f8f9fa;
  border-radius: 12px;
  padding: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  position: relative;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.06);
}

.guide-step:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.step-number {
  width: 32px;
  height: 32px;
  background-color: #3498db;
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  margin-bottom: 12px;
}

.step-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.step-text {
  margin: 0;
  font-size: 14px;
  color: #606266;
  line-height: 1.5;
}

@media (max-width: 768px) {
  .guide-container {
    flex-direction: column;
  }
  
  .guide-step {
    flex-direction: row;
    text-align: left;
    padding: 15px;
    margin-bottom: 8px;
  }
  
  .step-number {
    margin-bottom: 0;
    margin-right: 15px;
  }
  
  .stats-container {
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  }
  
  .stat-value {
    font-size: 18px;
  }
  
  .actions-section {
    flex-wrap: wrap;
  }
}
</style> 